package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import connection.JDCConnection;

import model.Facture;

public class FactureDAO {
	
	public Facture getFacturebyIdEtudiant(JDCConnection connection,int IdEtudiant) {
		Facture facture = new Facture();
		Statement stmt;
		ResultSet rs;
		
		try {

			stmt = connection.createStatement();

			rs = stmt.executeQuery("SELECT * FROM Facture where idEtudiant="+IdEtudiant);
			while (rs.next()) {

				facture.setIdFacture(rs.getInt("IdFacture"));
				facture.setIdEtudiant(rs.getInt("IdEtudiant"));
				facture.setMontant(rs.getDouble("montant"));
				facture.setDate(rs.getInt("date"));
				facture.setNumConfirmation(rs.getInt("numConfirmation"));
				facture.setDetails(rs.getString("details"));
				
				
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return facture;
	}
	
	public boolean saveFacture(JDCConnection connection,Facture facture) {
		Statement stmt;

		try {
			stmt = connection.createStatement();
		

			stmt.executeUpdate("INSERT INTO Facture(idEtudiant,montant,date,details,numConfirmation)" +
					" VALUES ('"+ facture.getIdEtudiant()+"','"
					+ facture.getMontant()+"','"
					+ facture.getDate()+ "','"
					+ facture.getDetails()+"','"
					+ facture.getNumConfirmation()+"')");
			
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
		return true;
	}

	public boolean payerFacture(JDCConnection connection, int idFacture, int numConfirmation, long date) {
		Statement stmt;
		try {
			stmt = connection.createStatement();
			stmt.executeUpdate("UPDATE Facture SET numConfirmation = '" + numConfirmation + "', date = '" + date + "' WHERE idFacture = '" + idFacture + "')");
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
}
